package controller;


import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Account;
import model.Customer;
import model.data.database.AccountTable;
import model.data.database.CustomerTable;

/** * Servlet implementation class AccountServlet */
public class AccountServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException 
	{
		AccountTable accountTable = new AccountTable();
		CustomerTable customerTable = new CustomerTable();
		
		try
		{
			Account account = new Account();
			account.setUserName(request.getParameter("txtEmail"));
			account.setPassword(request.getParameter("txtPassword"));
			accountTable.write(account);
			ArrayList<Account> accounts = (ArrayList<Account>)accountTable.getByUserName(request.getParameter("txtEmail"));
			account = accounts.get(0);
			
			Customer customer = new Customer();
			customer.setName(request.getParameter("txtFirstName"));
			customer.setAddress(request.getParameter("txtAddress"));
			customer.setAccount(account);
			customerTable.write(customer);
			
			response.sendRedirect("Index.jsp");
		}
		catch (Exception e)
		{
			response.getWriter().write("Oops!");
		}
	}
}
